package gq.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import gq.example.constant.MessageConstant;
import gq.example.dto.QuestionDto;
import gq.example.dto.QuestionPageQueryDto;
import gq.example.entity.Question;
import gq.example.mapper.QuestionMapper;
import gq.example.result.PageResult;
import gq.example.service.QuestionService;
import gq.example.vo.QuestionVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Resource
    private QuestionMapper questionMapper;

    /**
     * 添加问题
     * @param questionDto
     */
    @Override
    public void addQuestion(QuestionDto questionDto) {
        //创建问题对象
        Question question = Question.builder()
                .sector_id(questionDto.getSector_id()) // 绑定问题对应的部门
                .status(MessageConstant.INDICATES_NO_REPLY) // 默认未回复状态
                .build();

        //将数据拷贝给问题对象
        BeanUtils.copyProperties(questionDto, question);

        questionMapper.addQuestion(question);
    }

    /**
     * 历史记录
     * @param questionPageQueryDto
     * @return
     */
    @Override
    public PageResult historyPageQuery(QuestionPageQueryDto questionPageQueryDto) {
        PageHelper.startPage(questionPageQueryDto.getPage(), questionPageQueryDto.getSize());

        Page<QuestionVo> page = questionMapper.historyPageQuery(questionPageQueryDto);

        //封装结果集
        long total = page.getTotal();
        List<QuestionVo> records = page.getResult();

        return new PageResult(total, records);
    }

    /**
     * 意见查看
     * @param questionPageQueryDto
     * @return
     */
    @Override
    public PageResult listPageQuery(QuestionPageQueryDto questionPageQueryDto) {
        // 启动分页，并指定自定义计数方法
        PageHelper.startPage(questionPageQueryDto.getPage(), questionPageQueryDto.getSize());

        Page<Question> page = questionMapper.listPageQuery(questionPageQueryDto);

        long total = page.getTotal();
        List<Question> records = page.getResult();

        return new PageResult(total, records);
    }

    /**
     * 评价问题
     * @param id
     * @param assess
     */
    @Override
    public void evaluateQuestion(Integer id, String assess, Integer mark) {
        Question question = Question.builder()
                .id(id)
                .assess(assess)
                .mark(mark)
                .status(MessageConstant.INDICATES_AN_EVALUATION) // 状态设为已评价
                .build();

        questionMapper.update(question);
    }

    /**
     * 归档问题
     * @param id
     */
    @Override
    public void archiveQuestion(Integer id) {
        Question question = Question.builder()
                .id(id)
                .status(MessageConstant.INDICATES_ARCHIVE) // 状态设为已归档
                .build();

        questionMapper.update(question);
    }

    /**
     * 问题分派
     * @param questionDto
     */
    @Override
    public void update(QuestionDto questionDto,Integer id){
        Question question = Question.builder()
                .id(id)
                .build();
        BeanUtils.copyProperties(questionDto, question);

        questionMapper.update(question);
    }

    /**
     * 问题查看(前端回显)
     * @param id
     * @return
     */
    @Override
    public QuestionVo viewQuestion(Integer id) {
        return questionMapper.viewQuestion(id);
    }

    @Override
    public String getContentById(Integer id) {
        return questionMapper.getContentById(id);
    }

}
